package com.example.guardian.anticheatguard;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.guardian.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class viewpagerAdapter extends RecyclerView.Adapter<viewpagerAdapter.ViewPageViewHolder>  {
    private List<String> list = new ArrayList<String>();
    private List<ArrayList> total_list = new ArrayList<>();
    private Context mContext;
    private List<Integer> list_iamge = new ArrayList<>();



    @NonNull
    @Override
    public ViewPageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //解析item_pager.xml

        mContext = parent.getContext();
        View itemRoot = LayoutInflater.from(mContext).inflate(R.layout.viewpager2, parent, false);
        ViewPageViewHolder mViewHolder = new ViewPageViewHolder(itemRoot);

        return mViewHolder;
    }



    /**
     * 每次滑动就会触发一次
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull ViewPageViewHolder holder, @SuppressLint("RecyclerView") int position) {

        getdata();
        anti_adapter anti_adapter= new anti_adapter(mContext,total_list.get(position));
        holder.textView.setText(list.get(position));
        holder.imageView.setImageResource((Integer) list_iamge.get(position));
        holder.listView.setAdapter(anti_adapter);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "左右滑动", Toast.LENGTH_SHORT).show();
            }
        });
        anti_adapter.setOnItemSpreadClickListener(new anti_adapter.onItemSpreadListener() {
            @Override
            public void onSpreadClick(int i) {
                ArrayList<newslist> lists=total_list.get(position);
                String web = lists.get(i).getUri();
                Intent intent = new Intent(mContext, news_details.class);
                intent.putExtra("web", web);
                mContext.startActivity(intent);

            }
        });

    }

    private void getdata() {
        try {
            AssetManager assetManager = mContext.getAssets(); //获得assets资源管理器（assets中的文件无法直接访问，可以使用AssetManager访问）
            InputStreamReader inputStreamReader = new InputStreamReader(assetManager.open("case.json"),"UTF-8"); //使用IO流读取json文件内容
            BufferedReader br = new BufferedReader(inputStreamReader);//使用字符高效流
            String line;
            StringBuilder builder = new StringBuilder();
            while ((line = br.readLine())!=null){
                builder.append(line);
            }
            br.close();
            inputStreamReader.close();

            list_iamge.add((Integer) (R.drawable.case1));
            list_iamge.add((Integer) (R.drawable.case2));
            list_iamge.add((Integer) (R.drawable.case3));
            list_iamge.add((Integer) (R.drawable.case4));
            list_iamge.add((Integer) (R.drawable.case5));
            list_iamge.add((Integer) (R.drawable.case6));
            list_iamge.add((Integer) (R.drawable.case7));

            JSONObject testJson = new JSONObject(builder.toString()); // 从builder中读取了json中的数据。

            Iterator<String> sIterator = testJson.keys();
            while(sIterator.hasNext()) {
                String key = sIterator.next();
                list.add(key);
                JSONArray array = testJson.getJSONArray(key);
                List<newslist> newslistd = new ArrayList<>();
                for (int i = 0; i < array.length(); i++) {
                    JSONObject jsonObject = array.getJSONObject(i);
                    String name = jsonObject.getString("name");
                    String detail = jsonObject.getString("det");
                    String image = jsonObject.getString("image");
                    String url = jsonObject.getString("url");
                    newslist singelist = new newslist(name, detail, image, url);
                    newslistd.add(singelist);
                }
                total_list.add((ArrayList) newslistd);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }



    /**
     * 设置几个页面
     * @return
     */
    @Override
    public int getItemCount() {
        return 7;
    }



    /*用来获取item_pager.xml*/
    class ViewPageViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        RelativeLayout container;
        ListView listView;
        ImageView imageView;
        public ViewPageViewHolder(@NonNull View itemView) {
            super(itemView);
            container = itemView.findViewById(R.id.containerT);
            textView = itemView.findViewById(R.id.vp_title);
            listView = itemView.findViewById(R.id.anti_List_view);
            imageView = itemView.findViewById(R.id.vp_type_img);
        }
    }


}